package com.vv.tools.modules.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vv.tools.common.message.Result;
import com.vv.tools.common.util.TraceOp;
import com.vv.tools.common.validator.AddGroup;
import com.vv.tools.common.validator.UpdateGroup;
import com.vv.tools.modules.sys.converter.UserConvert;
import com.vv.tools.modules.sys.model.UserDTO;
import com.vv.tools.modules.sys.service.UserService;

import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author vv
 * @since 2025-02-19
 */
@Tag(name = "User Controller")
@RestController
@RequestMapping("/sys/user")
public class UserController {

    @Resource
    private UserService userService;

    @Operation(tags = "查询用户", description = "查询用户")
    @GetMapping("/{id}")
    public Result<UserDTO> query(@PathVariable @NotNull(message = "ID不能为空") Long id) {
        return Result.success(UserConvert.INSTANCE.do2dto(userService.getById(id)));
    }

    @Operation(tags = "新增用户", description = "新增用户")
    @PostMapping
    public Result<Boolean> save(@RequestBody @Validated(value = {AddGroup.class}) UserDTO userDTO) {
        return Result.success(userService.addUser(userDTO));
    }

    @Operation(tags = "删除用户", description = "删除用户")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable @NotNull(message = "ID不能为空") Long id) {
        return Result.success(userService.deleteUser(id));
    }

    @Operation(tags = "修改用户", description = "修改用户")
    @PutMapping
    public Result<Boolean> update(@RequestBody @Validated(value = {UpdateGroup.class}) UserDTO userDTO) {
        return Result.success(userService.updateUser(userDTO));
    }

    @Operation(tags = "用户列表", description = "用户列表")
    @GetMapping("/list")
    public Result<Page<UserDTO>> list(UserDTO userDTO) {
        return Result.success(userService.page(userDTO));
    }

    @Operation(tags = "查询当前用户", description = "查询当前用户")
    @GetMapping("/current")
    public Result<UserDTO> currentUser() {
        return Result.success(UserConvert.INSTANCE.do2dto(TraceOp.getUser()));
    }

}
